package org.soc.timesheet.domain;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.roo.addon.json.RooJson;
import org.springframework.transaction.annotation.Transactional;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;

@Entity
@Table(name = "PROJECTS")
@Configurable
@RooJavaBean
@RooJpaActiveRecord(versionField = "", table = "PROJECTS")
@RooDbManaged(automaticallyDelete = true)
@RooToString(excludeFields = { "subProjectss", "timesheets", "usersProjects" })
@RooJson
@JsonIgnoreProperties({ "subProjectss", "timesheets", "usersProjects" })
public class Projects {

    public static Projects fromJsonToProjects(String json) {
        try {
            return new ObjectMapper().readValue(json, Projects.class);
        } catch (JsonParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @PersistenceContext
    transient EntityManager entityManager;

    public static final List<String> fieldNames4OrderClauseFilter = java.util.Arrays.asList("");

    public static final EntityManager entityManager() {
        EntityManager em = new Projects().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

    public static long countProjectses() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Projects o", Long.class).getSingleResult();
    }

    public static List<Projects> findAllProjectses() {
        return entityManager().createQuery("SELECT o FROM Projects o", Projects.class).getResultList();
    }

    public static List<Projects> findAllProjectses(String sortFieldName, String sortOrder) {
        String jpaQuery = "SELECT o FROM Projects o";
        if (fieldNames4OrderClauseFilter.contains(sortFieldName)) {
            jpaQuery = jpaQuery + " ORDER BY " + sortFieldName;
            if ("ASC".equalsIgnoreCase(sortOrder) || "DESC".equalsIgnoreCase(sortOrder)) {
                jpaQuery = jpaQuery + " " + sortOrder;
            }
        }
        return entityManager().createQuery(jpaQuery, Projects.class).getResultList();
    }

    public static Projects findProjects(Integer id) {
        if (id == null) return null;
        return entityManager().find(Projects.class, id);
    }

    public static List<Projects> findProjectsEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Projects o", Projects.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

    public static List<Projects> findProjectsEntries(int firstResult, int maxResults, String sortFieldName, String sortOrder) {
        String jpaQuery = "SELECT o FROM Projects o";
        if (fieldNames4OrderClauseFilter.contains(sortFieldName)) {
            jpaQuery = jpaQuery + " ORDER BY " + sortFieldName;
            if ("ASC".equalsIgnoreCase(sortOrder) || "DESC".equalsIgnoreCase(sortOrder)) {
                jpaQuery = jpaQuery + " " + sortOrder;
            }
        }
        return entityManager().createQuery(jpaQuery, Projects.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

    @Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

    @Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Projects attached = Projects.findProjects(this.id);
            this.entityManager.remove(attached);
        }
    }

    @Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

    @Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

    @Transactional
    public Projects merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Projects merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

    @OneToMany(mappedBy = "idProject")
    private Set<SubProjects> subProjectss;

    @OneToMany(mappedBy = "idProject")
    private Set<Timesheet> timesheets;

    @OneToMany(mappedBy = "idProject")
    private Set<UsersProject> usersProjects;

    @Column(name = "DESC_PROJECT", length = 45)
    private String descProject;

    public Set<SubProjects> getSubProjectss() {
        return subProjectss;
    }

    public void setSubProjectss(Set<SubProjects> subProjectss) {
        this.subProjectss = subProjectss;
    }

    public Set<Timesheet> getTimesheets() {
        return timesheets;
    }

    public void setTimesheets(Set<Timesheet> timesheets) {
        this.timesheets = timesheets;
    }

    public Set<UsersProject> getUsersProjects() {
        return usersProjects;
    }

    public void setUsersProjects(Set<UsersProject> usersProjects) {
        this.usersProjects = usersProjects;
    }

    public String getDescProject() {
        return descProject;
    }

    public void setDescProject(String descProject) {
        this.descProject = descProject;
    }

    public String toString() {
        return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames("subProjectss", "timesheets", "usersProjects").toString();
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private Integer id;

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String toJson() {
        return new JSONSerializer().exclude("*.class").serialize(this);
    }

    public String toJson(String[] fields) {
        return new JSONSerializer().include(fields).exclude("*.class").serialize(this);
    }

    public static String toJsonArray(Collection<Projects> collection) {
        return new JSONSerializer().exclude("*.class").serialize(collection);
    }

    public static String toJsonArray(Collection<Projects> collection, String[] fields) {
        return new JSONSerializer().include(fields).exclude("*.class").serialize(collection);
    }

    public static Collection<Projects> fromJsonArrayToProjectses(String json) {
        return new JSONDeserializer<List<Projects>>().use("values", Projects.class).deserialize(json);
    }
}
